package com.health.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.health.common.entity.CheckItem;
import com.health.common.vo.QueryPageBean;
import com.health.common.vo.RespPageBean;
import com.health.mapper.CheckItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CheckItemService {

    @Autowired
    private CheckItemMapper checkItemMapper;

    public Integer addCheckItem(CheckItem checkItem) {
        return checkItemMapper.insert(checkItem);
    }

    public Integer deleteCheckItemById(Integer id) {
        return checkItemMapper.deleteById(id);
    }

    public Integer updateCheckItem(CheckItem checkItem) {
        return checkItemMapper.updateById(checkItem);
    }

    public List<CheckItem> getAllCheckItem() {
        return checkItemMapper.selectList(null);
    }

    public RespPageBean getCheckItemByPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        if(currentPage == null ){
            currentPage = 1;
        }
        if(pageSize == null){
            pageSize = 10;
        }

//        System.out.println("currentPage="+currentPage+",pageSize="+pageSize+",queryString="+queryString);

        QueryWrapper<CheckItem> wrapper = new QueryWrapper<>();
        if(queryString!= null){
            wrapper.and(w ->w.like("code",queryString).or().like("name",queryString));
        }

        Page<CheckItem> page = new Page<>(currentPage,pageSize);
        IPage<CheckItem> selectPage = checkItemMapper.selectPage(page, wrapper);

        RespPageBean respPageBean = new RespPageBean();
        respPageBean.setTotal(selectPage.getTotal());
        respPageBean.setData(selectPage.getRecords());
        return respPageBean;
    }
}
